package com.litxuf.logic.admin.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.litxuf.logic.admin.dao.model.UserPower;

public class UserPowerDAO extends HibernateDaoSupport  {

	public void deleteByUserId(final String userId,final String  powerType) {
		getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				List result = new ArrayList();
				String hql = "delete from UserPower t where t.userId ='"+userId +"' and t.powerType='"+powerType+"'"  ;
				Query query = session.createQuery(hql);
				result.add(new Integer(query.executeUpdate()));
				return result;
			}

		});

	}

	public List findPowerByUserIdAndType(String userId, String type) {
		 
		return this.getHibernateTemplate().find(" from UserPower t where t.userId='"+userId+"' and t.powerType='"+type+"'");
	}

	public String save(UserPower item) {
		String pk = null;
		try {
			pk = ((Integer) getHibernateTemplate().save(item)).toString();
		} catch (Exception e) {
			logger.error(ExceptionUtils.getFullStackTrace(e));
		}
		return pk;
	}

	 /**
	  * 查询后台所有栏目
	  * made by litxuf
	  * Dec 8, 2010-2:52:26 PM
	  * @return
	  */
	public List findAdminColumn() {

		return this.getHibernateTemplate().find("FROM AdminColumn");
	}
}
